package com.cincc.cinccimdemo.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import androidx.appcompat.widget.AppCompatTextView;

import com.cincc.cinccimdemo.common.callback.IAudioCallback;
import com.cincc.cinccimdemo.common.callback.IVoiceCallback;
import com.cincc.cinccimdemo.common.record.RecorderManager;
import com.cincc.cinccimdemo.common.record.VoiceInfo;

public class AudioTextView extends AppCompatTextView implements View.OnTouchListener {

    private static final String TAG = AudioTextView.class.getSimpleName();
    private static int MIN_RECORD_TIME = 1000;//最小录音间隔，1s
    private IAudioCallback audioCallback;

    VoiceArcView voiceArcView;
    Context context;

    public AudioTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        this.setOnTouchListener(this);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        Log.d(TAG, "onTouch: x = " + event.getRawX() + " y = " + event.getRawY());
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            if (voiceArcView.getVisibility() == View.GONE) {
                voiceArcView.setVisibility(View.VISIBLE);
                RecorderManager.getInstance().startRecord(context, new RecorderManager.OnAudioRecordListener() {
                    @Override
                    public void dbResult(double db) {
                        if (voiceArcView != null)
                            voiceArcView.setVoiceVolume(db);
                    }
                });
                voiceArcView.responseTouchEvent(event);
            }
        } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
            if (voiceArcView.getVisibility() == View.VISIBLE) {
                voiceArcView.responseTouchEvent(event);
            }
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            if (voiceArcView.getVisibility() == View.VISIBLE) {
                voiceArcView.responseTouchEvent(event);
            }
        }
        return true;
    }

    /**
     * 录音完成，并通知活动发送录音
     */
    private void sendRecord() {
        VoiceInfo audioInfo = RecorderManager.getInstance().stopRecord(false);
        if (audioInfo.getDuration() < 1) {
            if (audioCallback != null) {
                audioCallback.onRecordFinish(0, audioInfo);
            }
        } else {
            if (audioCallback != null) {
                audioCallback.onRecordFinish(1, audioInfo);
            }
        }
    }

    private void cancelRecord() {
        RecorderManager.getInstance().stopRecord(true);
    }

    public void setAudioCallback(VoiceArcView voiceArcView, IAudioCallback callback) {
        this.voiceArcView = voiceArcView;
        this.audioCallback = callback;
        this.voiceArcView.setVoiceCallback(new IVoiceCallback() {
            @Override
            public void onSuccess() {
                Log.d(TAG, "onSuccess: ");
                sendRecord();
            }

            @Override
            public void onCancel() {
                Log.d(TAG, "onCancel: ");
                cancelRecord();
            }
        });
    }

}
